﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Globalization;

namespace TimeReporter
{
    public class ProjectWeeklySummary
    {
        ProjectList _theProjects = ProjectList.getInstance();
        Dictionary<DayOfWeek, float> _timePerDay;
        ProjectObject _theProject;
        bool _UsePreviousWeek;

        public ProjectWeeklySummary()
        {
            _timePerDay = new Dictionary<DayOfWeek, float>();
        }

        public bool UsePreviousWeek
        {
            get { return _UsePreviousWeek; }
            set { _UsePreviousWeek = value; }
        }

        public void process()
        {
            DateTime[] theRange = getRange();
            
            Console.WriteLine("Start date is:" + theRange[0].ToString());
            Console.WriteLine("End date is:" + theRange[1].ToString());

            var ProjectsByWeek1 =
                from proj1 in _theProjects
                from date in proj1.theTasks
                where date.StartDateTime >= theRange[0] && date.StartDateTime <= theRange[1]
                select new { proj1.ProjectCode, date.StartDateTime, date.TimeSpent };

            var test =
                from k in ProjectsByWeek1
                group k by k.StartDateTime;
                
            var test2 = 
                from k2 in ProjectsByWeek1
                group k2 by new {k2.ProjectCode, k2.StartDateTime} into g
                select new {g.Key.ProjectCode, g.Key.StartDateTime, totalTime = g.Sum(k2 => k2.TimeSpent) };

            var test3 =
                from k3 in test2
                orderby k3.StartDateTime
                group k3 by k3.StartDateTime;

                //select new { g.Key, Star = g.Sum(e => e.TimeSpent) };
                //select proj2;
                
               
                //select new {Date = g.Key, TimeSpent = g.Sum(p => p.TimeSpent) };
                // new {ProjCode, TimeSpentMon, TimeSpentTues, TimeSpentWed, TimeSpentThur, TimeSpentFri}
            
            foreach (var x in test)
            {
                Console.WriteLine("Group by : {0}" , x.Key.ToShortDateString());

                foreach (var z in x)
                {
                    Console.WriteLine("Task: {0}, Start:{1}, time:{2}", z.ProjectCode, z.StartDateTime.ToShortDateString(), z.TimeSpent.ToString());
                }
            }

            foreach (var x2 in test2)
            {
                Console.WriteLine(x2.ToString());
            }

            foreach (var x3 in test3)
            {
                Console.WriteLine("Group by: {0}", x3.Key.ToShortDateString());

                foreach (var x4 in x3)
                {
                    
                    _timePerDay.Add(x4.StartDateTime.DayOfWeek, x4.totalTime);

                    Console.WriteLine("Project {0}, time {1}", x4.ProjectCode, x4.totalTime);
                }
            }

            Console.WriteLine("Done");       


            /*
            foreach (ProjectObject proj in _theProjects)
            {
                myBindingListView<TaskObject> x = proj.theTasks;

                List<float> dateselect =
                    (from y in x
                     where y.StartDateTime >= theRange[0] && y.StartDateTime <= theRange[1]
                     select y.TimeSpent).ToList();
                    
                   foreach (float d in dateselect)
                   {
                       Console.WriteLine("Size of selection is: " + dateselect.Count.ToString());
                       Console.WriteLine(d.ToString());
                   }

                    
                        
            }
             */
        }

        private DateTime[] getRange()
        {
            DayOfWeek today;
            DateTime[] range = new DateTime[2];
            Calendar theCal = CultureInfo.InvariantCulture.Calendar;
            DateTime now = DateTime.Today;
            
            CultureInfo myCu = new CultureInfo("se-SE");
            CalendarWeekRule firstweek = myCu.DateTimeFormat.CalendarWeekRule;
            DayOfWeek firstday = myCu.DateTimeFormat.FirstDayOfWeek;
           
            Console.WriteLine("First day is {0}", firstday);

            int thisweek = theCal.GetWeekOfYear(now, firstweek, firstday);

            

            if (_UsePreviousWeek)
            {
                // Note: set up for monday - friday workdays
                
                range[0] = FirstDayInWeek.GetFirstDateOfWeek(theCal.AddWeeks(now, -1));
                range[1] = range[0].AddDays(4);
                
            }
            else
            {
                today = DateTime.Today.DayOfWeek;
                range[0] = FirstDayInWeek.GetFirstDateOfWeek(DateTime.Today);
                range[1] = range[0].AddDays(4);
            }


            return range;

        }


    }
}
